var seq = 0;
var cur = "";
function listSPPResultByMonth(sppTable, sppData) {
	$("#sppByMonthTable").empty();
	
	// 创建收益大于等于零的表格标题
	createSPPTitle(sppTable);
	
	var sppDto;
	seq = 0;
	// 首先创建收益大于等于0的表格
	for (var i = 0; i < sppData.list.length; i++){
		sppDto = sppData.list[i];
		cur = sppDto.currency;
		accumulationSubTotal(sppDto);
		createSppDataBody(sppTable, sppDto);
	}
	// 创建本币小计行
	createSppDataSubtotal(1, sppTable);
	
	// 创建标准币小计行
	createSppDataSubtotal(0, sppTable);
	
	dealNaN();
	$("#lblCur").text(cur);
}

function creatTD(tr,index, html, align, color, bgcolor) {
	var newTd = tr.insertCell(index);
	newTd.innerHTML = html;
	newTd.setAttribute('align', align);
	if (color) {
		newTd.setAttribute('style', 'color: ' + color);
	}
	if (bgcolor) {
		newTd.setAttribute('bgColor', bgcolor);
	}
	return newTd;
}
/**
 * 创建spp报表的数据部分
 * @param sppTable
 * @param sppDto
 * @returns
 */
function createSppDataBody(sppTable, sppDto) {
	seq++;
	var newTr = sppTable.insertRow(sppTable.rows.length);
	
	// 创建序号单元格
	var newTd = creatTD(newTr, 0, seq, 'center');
	
	// 创建月份名称单元格
	newTd = creatTD(newTr, 1, sppDto.settleMonth, 'center');
	
	// 创建Bets单元格
	newTd = creatTD(newTr, 2, sppDto.orderNum, 'center');
	
	// 创建Wins单元格
	newTd = creatTD(newTr, 3, sppDto.winOrder, 'center');
	
	// 创建Lose单元格
	newTd = creatTD(newTr, 4, sppDto.loseOrder, 'center');
	
	// 创建Draw单元格
	newTd = creatTD(newTr, 5, sppDto.drawOrder, 'center');
	
	// 创建Avg Stake单元格
	newTd = creatTD(newTr, 6, formNumber(sppDto.avgItamt, 0), 'right', '#008000');
	
	// 创建Turnover单元格
	newTd = creatTD(newTr, 7, formNumber(sppDto.matchamt, 0), 'right', '#008000');
	
	// 创建W/L单元格
	newTd = creatTD(newTr, 8, formNumber(sppDto.profit, 0), 'right', '#0000FF');
	
	if (sppDto.profit > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (sppDto.profit < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	else 
		newTd.innerHTML = "-";	
	
	// 创建Win(%)单元格
	newTd = creatTD(newTr, 9, formNumber(sppDto.perRate * 100, 0), 'center', '#0000FF');
	if (sppDto.perRate > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (sppDto.perRate < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	else 
		newTd.innerHTML = "-";	
	
	// 创建BackBet%单元格
	newTd = creatTD(newTr, 10, formNumber(sppDto.buyPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.buyPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建LayBet%单元格
	newTd = creatTD(newTr, 11, formNumber(sppDto.salePerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.salePerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建Bet%单元格
	newTd = creatTD(newTr, 12, formNumber(sppDto.betPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.betPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建Fav%单元格
	newTd = creatTD(newTr, 13, formNumber(sppDto.upPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.upPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建UD%单元格
	newTd = creatTD(newTr, 14, formNumber(sppDto.downPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.downPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建DNB%单元格
	newTd = creatTD(newTr, 15, formNumber(sppDto.drawPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.drawPerWin == 0)
		newTd.innerHTML = "-";		
	
	// 创建OV%单元格
	newTd = creatTD(newTr, 16, formNumber(sppDto.bigPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.bigPerWin == 0)
		newTd.innerHTML = "-";	
	
	// 创建UN%单元格
	newTd = creatTD(newTr, 17, formNumber(sppDto.smallPerWin * 100, 0), 'center', '#0000FF');
	if (sppDto.smallPerWin == 0)
		newTd.innerHTML = "-";	
}

/**
 * 创建spp报表的汇总行，包括本币和标准币
 * @param flag 1-本币小计 0 标准币小计
 * @param sppTable
 * @returns
 */
function createSppDataSubtotal(flag, sppTable) {

	var newTr = sppTable.insertRow(sppTable.rows.length);
	
	var newTd = creatTD(newTr, 0, "<strong>" + "Grand Total in CNY" + "</strong>", 'right', '#000000', '#F5F5DC');
	if (flag !=0) {
		newTd.innerHTML = "<strong>" + "Total Calculated in " + cur +  "</strong>";
	} 
	newTd.setAttribute('colspan', '2');
	
	// 创建Bets单元格
	newTd = creatTD(newTr, 1, "<strong>" + subTotalBets + "</strong>", 'center');
	
	// 创建Wins单元格
	newTd = creatTD(newTr, 2, "<strong>" + subTotalWins + "</strong>", 'center');
	
	// 创建Lose单元格
	newTd = creatTD(newTr, 3, "<strong>" + subTotalLose + "</strong>", 'center');
	
	// 创建Draw单元格
	newTd = creatTD(newTr, 4, "<strong>" + subTotalDraw + "</strong>", 'center');
	
	// 创建Avg Stake单元格
	newTd = creatTD(newTr, 5, "<strong>" + 
			formNumber(flag == 0 ? subTotalStStake / subTotalBets : subTotalStake / subTotalBets, 0) + 
			"</strong>", 'right', '#008000');
	
	// 创建Turnover单元格
	newTd = creatTD(newTr, 6, "<strong>" + formNumber(flag == 0 ? subTotalStmatchamt : subTotalMatchamt, 0) + "</strong>", 
			'right', '#008000');
	
	// 创建W/L单元格
	newTd = creatTD(newTr, 7, "<strong>" + formNumber(flag == 0 ? subTotalStProfit : subTotalProfit, 0) + "</strong>", 
			'right', '#0000FF');
	if (subTotalStProfit > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (subTotalStProfit < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	
	var tmpTotalProfit = flag == 0 ? subTotalStProfit : subTotalProfit;
	var tmpTotalMatchamt = flag == 0 ? subTotalStmatchamt : subTotalMatchamt; 
	// 创建Win(%)单元格
	newTd = creatTD(newTr, 8, "<strong>" + formNumber(tmpTotalProfit*100 / tmpTotalMatchamt, 0) + "</strong>", 
			'center', '#0000FF');
	if (tmpTotalProfit > 0)
		newTd.setAttribute('style', 'color: #0000FF');
	else if (tmpTotalProfit < 0)
		newTd.setAttribute('style', 'color: #FF0000');
	
	// 创建BackBet%单元格
	newTd = creatTD(newTr, 9, "<strong>" + formNumber(subTotalBuyWinOrders * 100 / subTotalBuyOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建LayBet%单元格
	newTd = creatTD(newTr, 10, "<strong>" + formNumber(subTotalLayWinOrders * 100 / subTotalLayOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建Bet%单元格
	newTd = creatTD(newTr, 11, "<strong>" + formNumber(subTotalWins * 100 / subTotalBets, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建Fav%单元格
	newTd = creatTD(newTr, 12, "<strong>" + formNumber(subTotalUpWinOrders * 100 / subTotalUpOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建UD%单元格
	newTd = creatTD(newTr, 13, "<strong>" + formNumber(subTotalDownWinOrders * 100 / subTotalDownOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建DNB%单元格
	newTd = creatTD(newTr, 14, "<strong>" + formNumber(subTotalDrawWinOrders * 100 / subTotalDrawOrders, 0) + "</strong>", 
			'center', '#0000FF');
	if (newTd.innerHTML.indexOf('NaN') != -1) {
		newTd.innerHTML = '-';
	}
		
	
	// 创建OV%单元格
	newTd = creatTD(newTr, 15, "<strong>" + formNumber(subTotalBigWinOrders * 100 / subTotalBigOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
	// 创建UN%单元格
	newTd = creatTD(newTr, 16, "<strong>" + formNumber(subTotalSmallWinOrders * 100 / subTotalSmallOrders, 0) + "</strong>", 
			'center', '#0000FF');
	
}

function createSPPTitle(sppTable) {
	// 创建行
	var newTr = sppTable.insertRow(sppTable.rows.length);
	newTr.setAttribute('bgColor', '#99BBFF');
	
	var newTd = creatTD(newTr, 0, "<strong>" + "#" + "</strong>", 'center');
	newTd.setAttribute('width', "30");
	newTd = creatTD(newTr, 1, "<strong>" + "Month" + "</strong>", 'center');
	newTd.setAttribute('width', "120");
	newTd = creatTD(newTr, 2, "<strong>" + "Bets" + "</strong>", 'center');
	newTd = creatTD(newTr, 3, "<strong>" + "Wins" + "</strong>", 'center');
	newTd = creatTD(newTr, 4, "<strong>" + "Lose" + "</strong>", 'center');
	newTd = creatTD(newTr, 5, "<strong>" + "Draw" + "</strong>", 'center');
	newTd = creatTD(newTr, 6, "<strong>" + "Avg Stake" + "</strong>", 'center');
	newTd = creatTD(newTr, 7, "<strong>" + "Turnover" + "</strong>", 'center');
	newTd = creatTD(newTr, 8, "<strong>" + "W/L" + "</strong>", 'center');
	newTd = creatTD(newTr, 9, "<strong>" + "Win%" + "</strong>", 'center');
	newTd = creatTD(newTr, 10, "<strong>" + "Back%" + "</strong>", 'center');
	newTd = creatTD(newTr, 11, "<strong>" + "Lay%" + "</strong>", 'center');
	newTd = creatTD(newTr, 12, "<strong>" + "Bet%" + "</strong>", 'center');
	newTd = creatTD(newTr, 13, "<strong>" + "Fav%" + "</strong>", 'center');
	newTd = creatTD(newTr, 14, "<strong>" + "UD%" + "</strong>", 'center');
	newTd = creatTD(newTr, 15, "<strong>" + "DNB%" + "</strong>", 'center');
	newTd = creatTD(newTr, 16, "<strong>" + "OV%" + "</strong>", 'center');
	newTd = creatTD(newTr, 17, "<strong>" + "UN%" + "</strong>", 'center');
}

//数字填充，为了提高处理性能定义的填充字符串，如果填充更多只需要修改这个变量即可
var fillNum = "0000000000"; //注：为了不影响不被替换，建议定义为对象属性。
		    //jquery.sgfmtable.js插件中可通过在页面定义 sgfm.fillNum 的属性进行替换。
//格式化数字111,111.00 ，不是数字类型不进行格式化,只支持16位精度
// 参数str:需要格式化的数字，不是数字类型不进行格式化
// 参数num:小数点保留多少位，不是数字或不传为默认保留两位
function formNumber(str,num){
	if(!isNaN(str) && $.trim(str) !==""){
	var html = str.toString(),digits=num,prefix=str<0?"-":"";
	if(!(/^[\d]+$/.test(digits))){
		digits = 2;
	}
	var pow10 = Math.pow(10,digits);
	html = Math.abs(Math.round(parseFloat(html)*pow10)/pow10).toString();
	var index = html.indexOf("."),sDigits="",length=html.length;
	if(index===-1){index=length++;sDigits=".";}
	if(digits>0){
		var inum = digits-(length-index-1);
		if(inum>0){
			sDigits += fillNum.substr(0,inum);//不足保留小数位补0 ,“fillNum”要与定义的变量相同
			html+=sDigits;
		}
	}
	if(index===0){
		html = "0"+ html;
	}else if(index > 3){
		var inum = Math.ceil(index/3)-1,text=html.substr(index-3),tmpText=html.substring(0,index-3);
		for(var i=1;i<inum;i++){
			var tmpLength=tmpText.length;
			text = tmpText.substr(tmpLength-3) + text;
			//text = tmpText.substr(tmpLength-3) + "," + text;
			tmpText = tmpText.substring(0,tmpLength-3);
		}
		html = tmpText+text;
		//html = tmpText+","+text;
		}
		return prefix+html;
	}
	return str;
}

/**
 * 创建表空行
 * @param table
 * @param colspan
 * @returns
 */
function createBlankBR(table, colspan) {
	var newTr = table.insertRow(table.rows.length);
	
	// 创建单元格
	var newTd = newTr.insertCell(0);
	newTd.innerHTML = "&nbsp;";
	newTd.setAttribute('colspan', colspan);
}

/**
 * subTotalBets 注单数小计
 * subTotalWins 注单数（赢单）小计
 * subTotalLose 注单数（输单）小计
 * subTotalDraw 注单数（平单）小计
 * 
 * subTotalStake 标准币投注额度小计
 * 
 * subTotalStmatchamt 标准币成交额度小计
 * 
 * subTotalAvgStake 标准币平均投注额度小计
 * 
 * subTotalStProfit 标准币收益小计
 * 
 */
var subTotalBets = 0, subTotalWins = 0, subTotalLose = 0, subTotalDraw = 0, 
	subTotalBuyOrders = 0, subTotalBuyWinOrders = 0, subTotalLayOrders = 0, subTotalLayWinOrders = 0,
	subTotalUpOrders = 0, subTotalUpWinOrders = 0, subTotalDownOrders = 0, subTotalDownWinOrders = 0,
	subTotalDrawOrders = 0, subTotalDrawWinOrders = 0, subTotalBigOrders = 0, subTotalBigWinOrders = 0,
	subTotalSmallOrders = 0, subTotalSmallWinOrders = 0, 
	subTotalStake = 0, subTotalStStake = 0, subTotalProfit = 0, subTotalStProfit = 0, 
	subTotalMatchamt = 0, subTotalStmatchamt = 0;

/**
 * 累加小计相关变量
 * @param sppDto 数据对象
 */
function accumulationSubTotal(sppDto) {
	subTotalBets += sppDto.orderNum;
	subTotalWins += sppDto.winOrder;
	subTotalLose += sppDto.loseOrder;
	subTotalDraw += sppDto.drawOrder;
	
	subTotalBuyOrders += sppDto.buyOrders;
	subTotalBuyWinOrders += sppDto.buyWinOrders;
	subTotalLayOrders += sppDto.layOrders;
	subTotalLayWinOrders += sppDto.layWinOrders;
	subTotalUpOrders += sppDto.upOrders;
	subTotalUpWinOrders += sppDto.upWinOrders;
	subTotalDownOrders += sppDto.downOrders; 
	subTotalDownWinOrders += sppDto.downWinOrders; 
	subTotalDrawOrders += sppDto.drawOrders;  
	subTotalDrawWinOrders += sppDto.drawWinOrders;  
	subTotalBigOrders += sppDto.bigOrders;  
	subTotalBigWinOrders += sppDto.bigWinOrders;  
	subTotalSmallOrders += sppDto.smallOrders;  
	subTotalSmallWinOrders += sppDto.smallWinOrders;  
	
	subTotalStake, subTotalStStake, subTotalProfit, subTotalStProfit, subTotalMatchamt, subTotalStmatchamt;
	subTotalStake += sppDto.itamt;
	subTotalStStake += sppDto.stamt;
	subTotalProfit += sppDto.profit;
	subTotalStProfit += sppDto.stprofit;
	subTotalMatchamt += sppDto.matchamt;
	subTotalStmatchamt += sppDto.stmatchamt;	
}

function dealNaN() {
	$("#sppByMonthTable td").each(function(){
	    if ($(this).html().indexOf('NaN') != -1) {
	    	$(this).html('-');
	    }

	})
}